package com.sirun.ble.receivers;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.sirun.ble.constants.BleConfig;
import com.sirun.ble.enums.BleEnum;
import com.sirun.ble.utils.AwLog;
import com.sirun.ble.manager.BleManager;

import static com.sirun.ble.constants.BleConfig.BleSdkTag;
import static com.sirun.ble.constants.BleConfig.CMD_TYPE_DEFAULT;
import static com.sirun.ble.constants.BleConfig.TAG_CONNECTED_NONE;

/**
 * @author : hzw
 */
public class BleSystemStatusReceiver extends BroadcastReceiver {

    private BleManager mBleManager;

    public BleSystemStatusReceiver(BleManager manager) {
        this.mBleManager = manager;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AwLog.d("onReceive---------");
        switch (intent.getAction()) {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                switch (blueState) {
                    case BluetoothAdapter.STATE_TURNING_ON:
                        AwLog.d(BleSdkTag, "SystemStatusReceiver onReceive---------STATE_TURNING_ON");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        AwLog.d(BleSdkTag, "SystemStatusReceiver onReceive---------STATE_ON");
                        BleConfig.isSystemBleOpen = true;
                        if(mBleManager.mBleSystemStatusListener != null) {
                            mBleManager.mBleSystemStatusListener.bleOpen();
                        }
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        AwLog.d(BleSdkTag, "SystemStatusReceiver onReceive---------STATE_TURNING_OFF");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        AwLog.d(BleSdkTag, "SystemStatusReceiver onReceive---------STATE_OFF");
                        BleConfig.isSystemBleOpen = false;
                        if(mBleManager.mBleSystemStatusListener != null) {
                            mBleManager.mBleSystemStatusListener.bleClose();
                        }
                        if(mBleManager.mBleConnectListener != null && mBleManager.getConnectStatus() != TAG_CONNECTED_NONE) {
                            mBleManager.mBleConnectListener.connectFail(null, BleEnum.BLE_COMMON_SYSTEM_CLOSED.code, BleEnum.BLE_COMMON_SYSTEM_CLOSED.desc);
                        }
                        mBleManager.closeConnect();
                        break;
                }
                break;
        }
    }
}
